package org.kinglone.weixin.kit;

import net.sf.json.JSONObject;

import org.kinglone.weixin.model.AccessToken;
import org.kinglone.weixin.model.WeixinFinalValue;
import org.kinglone.weixin.servlet.WeixinContext;
import org.springframework.stereotype.Component;
/**
 * 刷新token，需要执行的代码
 * @author Administrator
 *
 */
@Component
public class RefreshAccessTokenKit {
	
	public static String refreshToken() {
		
		String url = WeixinFinalValue.ACCESS_TOKEN_URL;
		url = url.replaceAll("APPID", WeixinContext.getInstance().getAppId());
		url = url.replaceAll("APPSECRET", WeixinContext.getInstance().getAppSecurt());
		System.out.println("token的url:"+url);		
		String content = WeixinBasicKit.sendGet(url);	
		try {
			 //1、使用JSONObject
	        JSONObject jsonObject=JSONObject.fromObject(content);
	        AccessToken at=(AccessToken)JSONObject.toBean(jsonObject, AccessToken.class); 
	        WeixinContext.getInstance().setAccessToken(at);
			System.out.println("基础access_token为:"+at.getAccess_token()+"--有效期为："+at.getExpires_in());	
			return at.getAccess_token();
		} catch (Exception e) {
			e.printStackTrace();
			refreshToken();
		}
		return null;
							
	}
}
